package com.icongyou.enterprise.talent_marking;

import com.icongyou.enterprise.common.result.Result;
import com.icongyou.enterprise.talent_marking.entity.dto.MarkPotential;
import com.icongyou.enterprise.talent_marking.service.HighPotentialSubmissionService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class HighPotentialSubmissionServiceTest {

    @Mock
    private HighPotentialSubmissionService highPotentialSubmissionService;

    private String userId;
    private String submissionId;
    private MarkPotential markPotential;

    @BeforeEach
    void setUp() {
        userId = "USER001";
        submissionId = "SUBM001";

        markPotential = new MarkPotential();
        markPotential.setUserId(userId);
        markPotential.setSubmissionId(submissionId);
        markPotential.setRemark("非常有潜力的作品");
    }

    @Test
    void markHighPotential_WithValidRequest_ShouldReturnSuccess() {
        // Arrange
        Result<String> expectedResult = Result.success("标记成功");
        when(highPotentialSubmissionService.markHighPotential(any(MarkPotential.class))).thenReturn(expectedResult);

        // Act
        Result<String> result = highPotentialSubmissionService.markHighPotential(markPotential);

        // Assert
        assertNotNull(result);
        assertEquals(200, result.getCode());
        assertEquals("标记成功", result.getData());

        verify(highPotentialSubmissionService).markHighPotential(markPotential);
    }

    @Test
    void updateRemark_WithValidRequest_ShouldReturnSuccess() {
        // Arrange
        Result<String> expectedResult = Result.success("备注更新成功");
        when(highPotentialSubmissionService.updateRemark(any(MarkPotential.class))).thenReturn(expectedResult);

        // Act
        Result<String> result = highPotentialSubmissionService.updateRemark(markPotential);

        // Assert
        assertNotNull(result);
        assertEquals(200, result.getCode());
        assertEquals("备注更新成功", result.getData());

        verify(highPotentialSubmissionService).updateRemark(markPotential);
    }

    @Test
    void unmarkHighPotential_WithValidRequest_ShouldReturnSuccess() {
        // Arrange
        Result<String> expectedResult = Result.success("取消标记成功");
        when(highPotentialSubmissionService.unmarkHighPotential(any(MarkPotential.class))).thenReturn(expectedResult);

        // Act
        Result<String> result = highPotentialSubmissionService.unmarkHighPotential(markPotential);

        // Assert
        assertNotNull(result);
        assertEquals(200, result.getCode());
        assertEquals("取消标记成功", result.getData());

        verify(highPotentialSubmissionService).unmarkHighPotential(markPotential);
    }

    @Test
    void getMyPotentialMarks_ShouldReturnMarkList() {
        // Arrange - 使用Map或Object列表来模拟返回数据
        List<Object> marks = Arrays.asList(
                createMockSubmission("SUBM001", "项目A"),
                createMockSubmission("SUBM002", "项目B")
        );
        Result<List<Object>> expectedResult = Result.success(marks);
        when(highPotentialSubmissionService.getMyPotentialMarks(userId)).thenReturn((Result) expectedResult);

        // Act
        Result<?> result = highPotentialSubmissionService.getMyPotentialMarks(userId);

        // Assert
        assertNotNull(result);
        assertEquals(200, result.getCode());
        assertNotNull(result.getData());
        assertTrue(result.getData() instanceof List);
        assertEquals(2, ((List<?>) result.getData()).size());

        verify(highPotentialSubmissionService).getMyPotentialMarks(userId);
    }

    @Test
    void getHighPotentialSubmissionInfo_ShouldReturnSubmissionInfo() {
        // Arrange - 使用Map或Object列表来模拟返回数据
        List<Object> submissionInfo = Arrays.asList(createMockSubmission("SUBM001", "项目A"));
        Result<List<Object>> expectedResult = Result.success(submissionInfo);
        when(highPotentialSubmissionService.getHighPotentialSubmissionInfo(userId)).thenReturn((Result) expectedResult);

        // Act
        Result<?> result = highPotentialSubmissionService.getHighPotentialSubmissionInfo(userId);

        // Assert
        assertNotNull(result);
        assertEquals(200, result.getCode());
        assertNotNull(result.getData());
        assertTrue(result.getData() instanceof List);
        assertEquals(1, ((List<?>) result.getData()).size());

        verify(highPotentialSubmissionService).getHighPotentialSubmissionInfo(userId);
    }

    @Test
    void isSubmissionMarked_WhenMarked_ShouldReturnTrue() {
        // Arrange
        when(highPotentialSubmissionService.isSubmissionMarked(userId, submissionId)).thenReturn(true);

        // Act
        boolean result = highPotentialSubmissionService.isSubmissionMarked(userId, submissionId);

        // Assert
        assertTrue(result);

        verify(highPotentialSubmissionService).isSubmissionMarked(userId, submissionId);
    }

    @Test
    void isSubmissionMarked_WhenNotMarked_ShouldReturnFalse() {
        // Arrange
        when(highPotentialSubmissionService.isSubmissionMarked(userId, submissionId)).thenReturn(false);

        // Act
        boolean result = highPotentialSubmissionService.isSubmissionMarked(userId, submissionId);

        // Assert
        assertFalse(result);

        verify(highPotentialSubmissionService).isSubmissionMarked(userId, submissionId);
    }

    @Test
    void markHighPotential_WithDuplicateMark_ShouldReturnError() {
        // Arrange
        Result<String> expectedResult = Result.error("该作品已被标记");
        when(highPotentialSubmissionService.markHighPotential(any(MarkPotential.class))).thenReturn(expectedResult);

        // Act
        Result<String> result = highPotentialSubmissionService.markHighPotential(markPotential);

        // Assert
        assertNotNull(result);
        assertEquals(500, result.getCode());
        assertEquals("该作品已被标记", result.getMessage());

        verify(highPotentialSubmissionService).markHighPotential(markPotential);
    }

    @Test
    void markHighPotential_WithCustomErrorCode_ShouldReturnCorrectCode() {
        // Arrange
        Result<String> expectedResult = Result.error(400, "参数错误");
        when(highPotentialSubmissionService.markHighPotential(any(MarkPotential.class))).thenReturn(expectedResult);

        // Act
        Result<String> result = highPotentialSubmissionService.markHighPotential(markPotential);

        // Assert
        assertNotNull(result);
        assertEquals(400, result.getCode());
        assertEquals("参数错误", result.getMessage());

        verify(highPotentialSubmissionService).markHighPotential(markPotential);
    }

    // 辅助方法：创建模拟的提交对象
    private Object createMockSubmission(String id, String title) {
        // 使用Map来模拟对象
        return Map.of(
                "submissionId", id,
                "title", title,
                "score", 95.0,
                "courseName", "软件工程"
        );
    }
}